package sseGameApps.service.mdkbr;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slim3.datastore.Datastore;
import org.slim3.util.BeanUtil;

import com.google.appengine.api.datastore.Transaction;

import sseGameApps.meta.mdkbr.MdKanjiMeta;
import sseGameApps.model.mdkbr.MdKanji;
import sseGameApps.meta.mdkbr.MdUseKanjiMeta;
import sseGameApps.model.mdkbr.MdUseKanji;

public class MdKanjiService {

    private MdKanjiMeta l = new MdKanjiMeta();
    private MdUseKanjiMeta e = new MdUseKanjiMeta();

    public MdKanji registKanji(String mojiText, String userId) {

        boolean uniqueKanjiFlg = false;
        int rareCheck = 0;
        int barPoint = 0;
        int attackPoint = 0;
        String typePattern = null;
        String typePatternText = null;

        MdKanji data =
            Datastore.query(l).filter(l.mojiText.equal(mojiText)).asSingle();

        if (data != null) {
            return data;
        }

        MdKanji dataSel =
            Datastore
                .query(l)
                .sort(l.mojiNo.desc)
                .offset(0)
                .limit(1)
                .asSingle();

        int mojiNo = 1;

        if (dataSel != null) {
            mojiNo = dataSel.getMojiNo();
        }

        data = new MdKanji();

        while (!uniqueKanjiFlg) {

            rareCheck = (int) (Math.round(Math.random() * 20 + 0.5));

            if(rareCheck == 18){
                // 超高能力チャンス
                barPoint = (int) (Math.round(Math.random() * 2000 + 0.5)) + 800;
                attackPoint = (int) (Math.round(Math.random() * 500 + 0.5)) + 200;
            } else if(rareCheck > 15){
                // 高能力チャンス
                barPoint = (int) (Math.round(Math.random() * 1000 + 0.5)) + 500;
                attackPoint = (int) (Math.round(Math.random() * 300 + 0.5)) + 100;
            } else {
                barPoint = (int) (Math.round(Math.random() * 500 + 0.5)) + 200;
                attackPoint = (int) (Math.round(Math.random() * 100 + 0.5)) + 50;
            }

            switch ((int) (Math.round(Math.random() * 5 + 0.5))) {
            case 1:
                typePattern = "KI";
                typePatternText = "木";
                break;
            case 2:
                typePattern = "HI";
                typePatternText = "火";
                break;
            case 3:
                typePattern = "KIN";
                typePatternText = "金";
                break;
            case 4:
                typePattern = "SUI";
                typePatternText = "水";
                break;
            case 5:
                typePattern = "DO";
                typePatternText = "土";
                break;
            default:
                typePattern = "KI";
                typePatternText = "木";
            }

            dataSel =
                Datastore
                    .query(l)
                    .filter(
                        l.barPoint.equal(barPoint),
                        l.attackPoint.equal(attackPoint),
                        l.kanjiType.equal(typePattern))
                    .asSingle();

            if (dataSel == null) {
                uniqueKanjiFlg = true;
            }
        }

        data.setMojiNo(mojiNo);
        data.setMojiText(mojiText);
        data.setKanjiType(typePattern);
        data.setKanjiTypeText(typePatternText);
        data.setSummonCount(1);
        data.setFirstSummonUser(userId);
        data.setDescText("");
        data.setBarPoint(barPoint);
        data.setAttackPoint(attackPoint);

        Transaction tx = Datastore.beginTransaction();
        Datastore.put(data);
        tx.commit();

        return data;
    }

    public MdKanji registKanjiSel(String mojiText, String inLevel, String userId) {

        int[] arrayBarPoint = {10,41,111,231,361,521};
        int[] arrayBarPointRange = {30,69,119,129,159,329};
        int[] arrayAttackPoint = {3,16,41,81,131,211};
        int[] arrayAttackPointRange = {12,24,39,49,79,99};

        boolean uniqueKanjiFlg = false;
//        int rareCheck = 0;
        int barPoint = 0;
        int attackPoint = 0;
        Integer arrayIndex = 0;

        if (inLevel == null || "".equals(inLevel)){
            return null;
        } else {
            arrayIndex = Integer.parseInt(inLevel) - 1;
        }

        String typePattern = null;
        String typePatternText = null;

        MdKanji data =
            Datastore.query(l).filter(l.mojiText.equal(mojiText)).asSingle();

        if (data != null) {
            return data;
        }

        MdKanji dataSel =
            Datastore
                .query(l)
                .sort(l.mojiNo.desc)
                .offset(0)
                .limit(1)
                .asSingle();

        int mojiNo = 1;

        if (dataSel != null) {
            mojiNo = dataSel.getMojiNo();
        }

        data = new MdKanji();

        while (!uniqueKanjiFlg) {

            barPoint = (int) (Math.round(Math.random() * arrayBarPointRange[arrayIndex] + 0.5)) + arrayBarPoint[arrayIndex];
            attackPoint = (int) (Math.round(Math.random() * arrayAttackPointRange[arrayIndex] + 0.5)) + arrayAttackPoint[arrayIndex];

            switch ((int) (Math.round(Math.random() * 5 + 0.5))) {
            case 1:
                typePattern = "KI";
                typePatternText = "木";
                break;
            case 2:
                typePattern = "HI";
                typePatternText = "火";
                break;
            case 3:
                typePattern = "KIN";
                typePatternText = "金";
                break;
            case 4:
                typePattern = "SUI";
                typePatternText = "水";
                break;
            case 5:
                typePattern = "DO";
                typePatternText = "土";
                break;
            default:
                typePattern = "KI";
                typePatternText = "木";
            }

            dataSel =
                Datastore
                    .query(l)
                    .filter(
                        l.barPoint.equal(barPoint),
                        l.attackPoint.equal(attackPoint),
                        l.kanjiType.equal(typePattern))
                    .asSingle();

            if (dataSel == null) {
                uniqueKanjiFlg = true;
            }
        }

        data.setMojiNo(mojiNo);
        data.setMojiText(mojiText);
        data.setKanjiType(typePattern);
        data.setKanjiTypeText(typePatternText);
        data.setSummonCount(1);
        data.setFirstSummonUser(userId);
        data.setDescText("");
        data.setBarPoint(barPoint);
        data.setAttackPoint(attackPoint);
        data.setLevel(arrayIndex + 1);

        Transaction tx = Datastore.beginTransaction();
        Datastore.put(data);
        tx.commit();

        return data;
    }


    public MdKanji getMdKanji(String mojiText) {

        MdKanji data =
            Datastore.query(l).filter(l.mojiText.equal(mojiText)).asSingle();

        return data;
    }

    public MdUseKanji registMdUseKanji(Map<String, Object> input) {

        MdUseKanji data = new MdUseKanji();

        MdUseKanji oldData =
            Datastore
                .query(e)
                .filter(e.userId.equal(input.get("userId").toString()))
                .asSingle();

        if (oldData != null) {
            BeanUtil.copy(input, oldData);

            Transaction tx = Datastore.beginTransaction();
            Datastore.put(oldData);
            tx.commit();

            return oldData;

        } else {
            BeanUtil.copy(input, data);

            Transaction tx = Datastore.beginTransaction();
            Datastore.put(data);
            tx.commit();

            return data;

        }

    }

    public MdUseKanji getMdUseKanji(String userId) {

        MdUseKanji data =
            Datastore.query(e).filter(e.userId.equal(userId)).asSingle();

        return data;
    }

    public Map<String, Integer> getTypeSpecialPoint(MdUseKanji input) {

        Integer typeSpecialPoint1 = 0;
        Integer typeSpecialPoint2 = 0;
        Integer typeSpecialPoint3 = 0;
        Integer typeSpecialPoint4 = 0;
        Integer typeSpecialPoint5 = 0;

        Map<String, Integer> data = new HashMap<String, Integer>();

        if("KI".equals(input.getUsingKanjiType01())){
            typeSpecialPoint1++;
        }
        if("KI".equals(input.getUsingKanjiType02())){
            typeSpecialPoint1++;
        }
        if("KI".equals(input.getUsingKanjiType03())){
            typeSpecialPoint1++;
        }

        if("HI".equals(input.getUsingKanjiType01())){
            typeSpecialPoint2++;
        }
        if("HI".equals(input.getUsingKanjiType02())){
            typeSpecialPoint2++;
        }
        if("HI".equals(input.getUsingKanjiType03())){
            typeSpecialPoint2++;
        }

        if("KIN".equals(input.getUsingKanjiType01())){
            typeSpecialPoint3++;
        }
        if("KIN".equals(input.getUsingKanjiType02())){
            typeSpecialPoint3++;
        }
        if("KIN".equals(input.getUsingKanjiType03())){
            typeSpecialPoint3++;
        }

        if("DO".equals(input.getUsingKanjiType01())){
            typeSpecialPoint4++;
        }
        if("DO".equals(input.getUsingKanjiType02())){
            typeSpecialPoint4++;
        }
        if("DO".equals(input.getUsingKanjiType03())){
            typeSpecialPoint4++;
        }

        if("SUI".equals(input.getUsingKanjiType01())){
            typeSpecialPoint5++;
        }
        if("SUI".equals(input.getUsingKanjiType02())){
            typeSpecialPoint5++;
        }
        if("SUI".equals(input.getUsingKanjiType03())){
            typeSpecialPoint5++;
        }

        data.put("typeSpecialPoint1", typeSpecialPoint1);
        data.put("typeSpecialPoint2", typeSpecialPoint2);
        data.put("typeSpecialPoint3", typeSpecialPoint3);
        data.put("typeSpecialPoint4", typeSpecialPoint4);
        data.put("typeSpecialPoint5", typeSpecialPoint5);

        return data;

    }

    public Map<String, String> getPlayerKanjiData(MdUseKanji input) {

        Map<String, String> data = new HashMap<String, String>();

        MdKanji data01 = null;
        MdKanji data02 = null;
        MdKanji data03 = null;
        MdKanji data04 = null;
        MdKanji data05 = null;
        MdKanji data06 = null;
        MdKanji data07 = null;
        MdKanji data08 = null;

        if (input.getHolderKanji01() != null
            && !"".equals(input.getHolderKanji01())) {
            data01 =
                Datastore
                    .query(l)
                    .filter(l.mojiText.equal(input.getHolderKanji01()))
                    .asSingle();

            if (data01 != null) {
                data.put("kanji01Bp", data01.getBarPoint().toString());
                data.put("kanji01Ap", data01.getAttackPoint().toString());
                data.put("kanji01Type", data01.getKanjiType());
                data.put("kanji01TypeTxt", data01.getKanjiTypeText());
            } else {
                data.put("kanji01Bp", "0");
                data.put("kanji01Ap", "0");
                data.put("kanji01Type", "");
                data.put("kanji01TypeTxt", "");
            }
        } else {
            data.put("kanji01Bp", "0");
            data.put("kanji01Ap", "0");
            data.put("kanji01Type", "");
            data.put("kanji01TypeTxt", "");
        }

        if (input.getHolderKanji02() != null
            && !"".equals(input.getHolderKanji02())) {
            data02 =
                Datastore
                    .query(l)
                    .filter(l.mojiText.equal(input.getHolderKanji02()))
                    .asSingle();

            if (data02 != null) {
                data.put("kanji02Bp", data02.getBarPoint().toString());
                data.put("kanji02Ap", data02.getAttackPoint().toString());
                data.put("kanji02Type", data02.getKanjiType());
                data.put("kanji02TypeTxt", data02.getKanjiTypeText());
            } else {
                data.put("kanji02Bp", "0");
                data.put("kanji02Ap", "0");
                data.put("kanji02Type", "");
                data.put("kanji02TypeTxt", "");
            }
        } else {
            data.put("kanji02Bp", "0");
            data.put("kanji02Ap", "0");
            data.put("kanji02Type", "");
            data.put("kanji02TypeTxt", "");
        }

        if (input.getHolderKanji03() != null
            && !"".equals(input.getHolderKanji03())) {
            data03 =
                Datastore
                    .query(l)
                    .filter(l.mojiText.equal(input.getHolderKanji03()))
                    .asSingle();

            if (data03 != null) {
                data.put("kanji03Bp", data03.getBarPoint().toString());
                data.put("kanji03Ap", data03.getAttackPoint().toString());
                data.put("kanji03Type", data03.getKanjiType());
                data.put("kanji03TypeTxt", data03.getKanjiTypeText());
            } else {
                data.put("kanji03Bp", "0");
                data.put("kanji03Ap", "0");
                data.put("kanji03Type", "");
                data.put("kanji03TypeTxt", "");
            }
        } else {
            data.put("kanji03Bp", "0");
            data.put("kanji03Ap", "0");
            data.put("kanji03Type", "");
            data.put("kanji03TypeTxt", "");
        }

        if (input.getHolderKanji04() != null
            && !"".equals(input.getHolderKanji04())) {
            data04 =
                Datastore
                    .query(l)
                    .filter(l.mojiText.equal(input.getHolderKanji04()))
                    .asSingle();

            if (data04 != null) {
                data.put("kanji04Bp", data04.getBarPoint().toString());
                data.put("kanji04Ap", data04.getAttackPoint().toString());
                data.put("kanji04Type", data04.getKanjiType());
                data.put("kanji04TypeTxt", data04.getKanjiTypeText());
            } else {
                data.put("kanji04Bp", "0");
                data.put("kanji04Ap", "0");
                data.put("kanji04Type", "");
                data.put("kanji04TypeTxt", "");
            }
        } else {
            data.put("kanji04Bp", "0");
            data.put("kanji04Ap", "0");
            data.put("kanji04Type", "");
            data.put("kanji04TypeTxt", "");
        }

        if (input.getHolderKanji05() != null
            && !"".equals(input.getHolderKanji05())) {
            data05 =
                Datastore
                    .query(l)
                    .filter(l.mojiText.equal(input.getHolderKanji05()))
                    .asSingle();

            if (data05 != null) {
                data.put("kanji05Bp", data05.getBarPoint().toString());
                data.put("kanji05Ap", data05.getAttackPoint().toString());
                data.put("kanji05Type", data05.getKanjiType());
                data.put("kanji05TypeTxt", data05.getKanjiTypeText());
            } else {
                data.put("kanji05Bp", "0");
                data.put("kanji05Ap", "0");
                data.put("kanji05Type", "");
                data.put("kanji05TypeTxt", "");
            }
        } else {
            data.put("kanji05Bp", "0");
            data.put("kanji05Ap", "0");
            data.put("kanji05Type", "");
            data.put("kanji05TypeTxt", "");
        }

        if (input.getHolderKanji06() != null
            && !"".equals(input.getHolderKanji06())) {
            data06 =
                Datastore
                    .query(l)
                    .filter(l.mojiText.equal(input.getHolderKanji06()))
                    .asSingle();

            if (data06 != null) {
                data.put("kanji06Bp", data06.getBarPoint().toString());
                data.put("kanji06Ap", data06.getAttackPoint().toString());
                data.put("kanji06Type", data06.getKanjiType());
                data.put("kanji06TypeTxt", data06.getKanjiTypeText());
            } else {
                data.put("kanji06Bp", "0");
                data.put("kanji06Ap", "0");
                data.put("kanji06Type", "");
                data.put("kanji06TypeTxt", "");
            }
        } else {
            data.put("kanji06Bp", "0");
            data.put("kanji06Ap", "0");
            data.put("kanji06Type", "");
            data.put("kanji06TypeTxt", "");
        }

        if (input.getHolderKanji07() != null
            && !"".equals(input.getHolderKanji07())) {
            data07 =
                Datastore
                    .query(l)
                    .filter(l.mojiText.equal(input.getHolderKanji07()))
                    .asSingle();

            if (data07 != null) {
                data.put("kanji07Bp", data07.getBarPoint().toString());
                data.put("kanji07Ap", data07.getAttackPoint().toString());
                data.put("kanji07Type", data07.getKanjiType());
                data.put("kanji07TypeTxt", data07.getKanjiTypeText());
            } else {
                data.put("kanji07Bp", "0");
                data.put("kanji07Ap", "0");
                data.put("kanji07Type", "");
                data.put("kanji07TypeTxt", "");
            }
        } else {
            data.put("kanji07Bp", "0");
            data.put("kanji07Ap", "0");
            data.put("kanji07Type", "");
            data.put("kanji07TypeTxt", "");
        }

        if (input.getHolderKanji08() != null
            && !"".equals(input.getHolderKanji08())) {
            data08 =
                Datastore
                    .query(l)
                    .filter(l.mojiText.equal(input.getHolderKanji08()))
                    .asSingle();

            if (data08 != null) {
                data.put("kanji08Bp", data08.getBarPoint().toString());
                data.put("kanji08Ap", data08.getAttackPoint().toString());
                data.put("kanji08Type", data08.getKanjiType());
                data.put("kanji08TypeTxt", data08.getKanjiTypeText());
            } else {
                data.put("kanji08Bp", "0");
                data.put("kanji08Ap", "0");
                data.put("kanji08Type", "");
                data.put("kanji08TypeTxt", "");
            }
        } else {
            data.put("kanji08Bp", "0");
            data.put("kanji08Ap", "0");
            data.put("kanji08Type", "");
            data.put("kanji08TypeTxt", "");
        }

        return data;

    }
    public List<MdKanji> getListTypeMdKanji(String kanjiType) {

        List<MdKanji> list =
            Datastore.query(l).filter(l.kanjiType.equal(kanjiType),l.attackPoint.greaterThan(250),l.level.equal(6)).sort(l.attackPoint.desc).offset(0).limit(10).asList();

        return list;
    }
    public List<MdKanji> getListAttackMdKanji(Integer minAp, Integer maxAp) {

        List<MdKanji> list =
            Datastore.query(l).filter(l.attackPoint.greaterThanOrEqual(minAp),l.attackPoint.lessThanOrEqual(maxAp),l.level.equal(6)).sort(l.attackPoint.desc).offset(0).limit(10).asList();

        return list;
    }
}
